package com.nanhang.service.serviceimpl;

import com.nanhang.entity.Classes;
import com.nanhang.entity.Student;
import com.nanhang.mapper.ClassesMapper;
import com.nanhang.mapper.StudentMapper;
import com.nanhang.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
//import com.github.pagehelper.Page;
//import com.github.pagehelper.PageHelper;
//import com.nanhang.mapper.ClassesMapper;
//import com.nanhang.mapper.StudentMapper;
//import com.nanhang.service.StudentService;
//import com.nanhang.util.PageResult;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;

	@Override
	public List<Student> findAll() {
		List<Student> students = studentMapper.selectByExample(null);
		students.forEach(st -> {
			Classes classes = classesMapper.selectByPrimaryKey(st.getCid());
			st.setCname(classes.getCname());
		});
		return students;
	}

	// 分页查询
	@Override
	public PageResult<Student> findByPage(Integer page, Integer pageSize) {
		//1. 开始分页
		PageHelper.startPage(page,pageSize);
		// 2. 开始查询数据
		List<Student> students = this.findAll();
		// 3. 将查询得到的数据转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		// 4. 转换为PageResult对象
		return new PageResult<>(studentPage.getResult(),studentPage.getTotal());

	}
}
